<?php
namespace App\Common\Lib\Err\Exception;

use App\Common as com;

class Try_ extends com\Lib\LibBase {

	/** 1：异常级别-严重 */
	protected static $tryException_Severity = 1;
	/** 2：异常级别-警告 */
	protected static $tryException_Record = 2;
	/** 3：异常级别-普通 */
	protected static $tryException_Return = 3;

	/**
	 * 返回触发器
	 * @param string $msg
	 * @param number $code
	 * @return void
	 */
	protected static function returnTrigger( string $msg, int $code ){
		com\Lib\Err\GetBack::recordCourse( [
			'type' => 'Try',
			'code' => $code,
			'msg' => self::$tryException_Severity,
		], 2 );
	}

	/**
	 * 抛出异常-记录(级别：severity)并返回
	 * @param string $msg
	 * @param number $code
	 * @param string $TryException
	 * @throws
	 */
	public static function returnSeverity( string $msg , int $code = null ) {
		if( $code === null ) $code = com\Cla\Err\Code::$sysDataError;
		//
		self::returnTrigger( $msg, $code );
		//
		throw com\Lib\Err\Exception::getThrow( $code, $msg, [
			'data' => [
				'tryExceptionLevel' => self::$tryException_Severity,
			]
		] );
	}

	/**
	 * 抛出异常-记录(级别：warning)并返回
	 * @param string $msg
	 * @param number $code
	 * @param string $exception
	 * @throws
	 */
	public static function returnRecord( string $msg, int $code = null ) {
		if( $code === null ) $code = com\Cla\Err\Code::$sysDataError;
		//
		self::returnTrigger( $msg, $code );
		//
		throw com\Lib\Err\Exception::getThrow( $code, $msg, [
			'data' => [
				'tryExceptionLevel' => self::$tryException_Record,
			]
		] );
	}

	/**
	 * 抛出异常-返回
	 * @param string $msg
	 * @param number $code
	 * @param string $exception
	 * @throws
	 */
	public static function return( string $msg, int $code = null ) {
		if( $code === null ) $code = com\Cla\Err\Code::$sysDataError;
		//
		self::returnTrigger( $msg, $code );
		//
		throw com\Lib\Err\Exception::getThrow( $code, $msg, [
			'data' => [
				'tryExceptionLevel' => self::$tryException_Return,
			]
		] );
	}

	/**
	 * try异常处理
	 * @param \Throwable $e
	 * @param bool $forceThrow 是否强制(重新)抛出异常。[false][默认]
	 * @return mixed
	 * @throws \Throwable
	 */
	public static function dealWithException( \Throwable $e, bool $forceThrow = false ) {
		if( ! $e instanceof com\Lib\Err\BaseException\Throw_ ) throw $e;
		//
		$eData = $e->getData();
		if( ! isset( $eData['tryExceptionLevel'] ) ) throw $e;
		//
		$returnData = null;
		switch ( $eData['tryExceptionLevel'] ){
			case self::$tryException_Severity :// 记录(severity)并返回错误码
				$returnData = com\Lib\Err\GetBack::serious( $e->getCode() );
				break;
			case self::$tryException_Record : // 记录(warning)并返回错误码
				$returnData = com\Lib\Err\GetBack::warning( $e->getCode() );
				break;
			case self::$tryException_Return : // 返回错误码
				$returnData = $e->getCode();
				break;
		}
		//
		if( true == $forceThrow ){
			throw $e;
		}
		//
		return $returnData;
	}

	/**
	 * 获取try异常处理中的错误代码(不抛出新的异常)
	 * @param \Exception $e
	 * @param int $default_return 默认返回的错误码
	 * @return com\Lib\Err|int|mixed|null 如果不是“TryReturn”，则返回null(默认)
	 */
	public static function getTryReturnCode( \Exception $e, $default_return = null ){
		switch ( $e->getMessage() ){
			case self::$tryException_Severity :
				return com\Lib\Err\GetBack::serious( $e->getCode() );
			case self::$tryException_Record :
				return com\Lib\Err\GetBack::warning( $e->getCode() );
			case self::$tryException_Return :
				return $e->getCode();
			default:
				return $default_return;
		}
	}
}
